package com.hsr.web.controller;


import com.hsr.web.dto.UserDTO;
import com.hsr.web.enums.ResultCode;
import com.hsr.web.service.ITUserService;
import com.hsr.web.utils.ResultVOUtil;
import com.hsr.web.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tb
 * @since 2022-03-30
 */
@RestController
@Api(tags = "用户相关接口")
@RequestMapping("/user")
public class TUserController {

    @Autowired
    ITUserService userService;
    @Autowired
    UserDetailsService userDetailsService;

    @PutMapping("")
    @ApiOperation("更新用户")
    public ResultVO update(@RequestBody UserDTO dto) {
        userService.updateUser(dto);
        return ResultVOUtil.success();
    }

    @PostMapping("/test")
    @ApiOperation("测试")
    public String test(@RequestParam("file") MultipartFile multipartFile) {
        try {
            String type = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
            String fileName = "/apps/test/" + "abc" + type;
            File filePath = new File("/apps/test/");
            if (!filePath.exists()){
                filePath.mkdir();
            }
            multipartFile.transferTo(new File(fileName));
            return fileName;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

}
